package cn.iocoder.yudao.module.cdk.controller.admin.cdklog;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.cdk.controller.admin.cdklog.vo.CdklogPageReqVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdklog.vo.CdklogRespVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdklog.vo.CdklogSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdklog.CdklogDO;
import cn.iocoder.yudao.module.cdk.service.cdklog.CdklogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - cdk登录日志")
@RestController
@RequestMapping("/cdk/log")
@Validated
public class CdklogController {

    @Resource
    private CdklogService logService;

    @PostMapping("/create")
    @Operation(summary = "创建cdk登录日志")
    @PreAuthorize("@ss.hasPermission('cdk:log:create')")
    public CommonResult<Integer> createlog(@Valid @RequestBody CdklogSaveReqVO createReqVO) {
        return success(logService.createlog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新cdk登录日志")
    @PreAuthorize("@ss.hasPermission('cdk:log:update')")
    public CommonResult<Boolean> updatelog(@Valid @RequestBody CdklogSaveReqVO updateReqVO) {
        logService.updatelog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除cdk登录日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cdk:log:delete')")
    public CommonResult<Boolean> deletelog(@RequestParam("id") Integer id) {
        logService.deletelog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得cdk登录日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cdk:log:query')")
    public CommonResult<CdklogRespVO> getlog(@RequestParam("id") Integer id) {
        CdklogDO log = logService.getlog(id);
        return success(BeanUtils.toBean(log, CdklogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得cdk登录日志分页")
    @PreAuthorize("@ss.hasPermission('cdk:log:query')")
    public CommonResult<PageResult<CdklogRespVO>> getlogPage(@Valid CdklogPageReqVO pageReqVO) {
        PageResult<CdklogDO> pageResult = logService.getlogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CdklogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出cdk登录日志 Excel")
    @PreAuthorize("@ss.hasPermission('cdk:log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportlogExcel(@Valid CdklogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CdklogDO> list = logService.getlogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "cdk登录日志.xls", "数据", CdklogRespVO.class,
                        BeanUtils.toBean(list, CdklogRespVO.class));
    }

}